import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'
import { Toast } from 'vant';

Vue.use(Toast);
Vue.use(VueRouter)

const routes = [
    {
        name:'home',
        path:'/home',
        alias:'/',
        component: ()=> import('@/views/home/home.vue'),
    },
    {
        name:'coll',
        path:'/coll',
        component: ()=> import('@/views/coll/coll.vue'),
        meta:{ requiresAuth:true }
    },
    {
        name:'user',
        path:'/user',
        component: ()=> import('@/views/user/user.vue'),
        props:function (router) {
            return { userData:router.query.userData }
        },
        meta:{ requiresAuth:true }
    },
    {
        name:'login',
        path:'/login',
        component: ()=> import('@/views/login/login.vue')
    },
    {
        name:'search',
        path:'/search',
        component: ()=> import('@/views/search/search.vue')
    },
    {
        name:'choice',
        path:'/choice',
        component: ()=> import('@/views/choice/choice.vue'),
        props:function (router) {
            return { arrId:router.query.arrId }
        },
        meta:{ requiresAuth:true }
    },
    {
        name:'examin',
        path:'/examin',
        component: ()=> import('@/views/examin/examin.vue'),
        props:function (router) {
            return { id:router.query.id }
        },
        meta:{ requiresAuth:true }
    }
]

const router = new VueRouter({
    routes
})

// 解决ElementUI(其他UI也适用)导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
router.beforeEach((to,from,next) => {//全局前置路由拦截
    // console.log(store)//this.$store.state.apiDataModules.token
    if(to.meta.requiresAuth){//判断是否需要路由拦截
        let token = store.state.apiDataModules.token
        if(token){//判断是否有登陆状态
            next()
        }else{//没有登陆就重定向到登陆页面
            // next({path:"/login",replace:true})
            // console.log(router)
            Toast('请先登陆');
            router.replace({path:"/login"})
        }
    }else{//不需要拦截
        next()
    }
})

export default router